<?php

/**
 * 添加这些import语句
 * 逻辑操作
 * @author Shaun
 */

namespace Member\Model;

use Utility\Log\Logger;
use Utility\ServiceLocator\ServiceLocator;
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\InputFilterAwareInterface;
use Zend\InputFilter\InputFilterInterface;
use Member\Table\MemberTable;

class MemberModel implements InputFilterAwareInterface {

    protected $id;      //id
    protected $username;    //用户名
    protected $password;    //密码
    protected $email;       //邮箱
    protected $image;       //头像
    protected $nickname;    //昵称
    protected $last_name;   //姓氏
    protected $first_name;  //名字
    protected $url;         //个人主页
    protected $address;
    protected $inputFilter;

    /**
     * 重写接口方法
     */
    public
            function setInputFilter(InputFilterInterface $inputFilter) {
        throw new \Exception("Not used");
    }

    /**
     *  重写接口方法
     */
    public function getInputFilter() {
        if (!$this->inputFilter) {
            $inputFilter = new InputFilter();
            $inputFilter->add(array(
                'name' => 'username',
                'required' => true,
                'filters' => array(
                    array('name' => 'StripTags'),
                    array('name' => 'StringTrim'),
                ),
                'validators' => array(
                    array(
                        'name' => 'StringLength',
                        'options' => array(
                            'encoding' => 'UTF-8',
                            'min' => 4,
                            'max' => 100,
                        ),
                    ),
                ),
                'error_message' => ' 用户名必须填写!',
            ));

            $inputFilter->add(array(
                'name' => 'password',
                'required' => true,
                'filters' => array(
                    array('name' => 'StripTags'),
                    array('name' => 'StringTrim'),
                ),
                'validators' => array(
                    array(
                        'name' => 'StringLength',
                        'options' => array(
                            'encoding' => 'UTF-8',
                            'min' => 6,
                            'max' => 100,
                        ),
                    ),
                ),
                'error_message' => '密码必须填写!',
            ));
            $inputFilter->add(array(
                'name' => 'email',
                'required' => true,
                'validators' => array(
                    array(
                        'name' => 'StringLength',
                        'options' => array(
                            'encoding' => 'UTF-8',
                            'min' => 8,
                            'max' => 100,
                        ),
                    ),
                ),
                'error_message' => 'Email格式有误!',
            ));
            $inputFilter->add(array(
                'name' => 'first_name',
                'required' => false,
            ));
            $inputFilter->add(array(
                'name' => 'last_name',
                'required' => false,
            ));
            $inputFilter->add(array(
                'name' => 'location',
                'required' => false,
            ));
            $inputFilter->add(array(
                'name' => 'url',
                'required' => false,
            ));
            $inputFilter->add(array(
                'name' => 'address',
                'required' => false,
            ));
            $inputFilter->add(array(
                'name' => 'image',
                'required' => false,
            ));
            $inputFilter->add(array(
                'name' => 'id',
                'required' => false,
            ));
            $this->inputFilter = $inputFilter;
        }

        return $this->inputFilter;
    }

    /* ---------------------------------调用区------------------------------------------- */

    /**
     *
     * @return MemberTable
     */
    protected static function getTableGateway() {
        $sm = ServiceLocator::getServiceLocator();
        return $sm->get("Member\Model\MemberTable");
    }

    /**
     * 插入一条新的数据或者修改
     * @param insert [admin]
     * @return MemberTable
     */
    public function Insert() {
        $tb = self::getTableGateway();
        $data = array(
            'username' => $this->username,
            'password' => MD5($this->password),
            'email' => $this->email,
            'last_name' => $this->last_name,
            'first_name' => $this->first_name,
            'address' => $this->address,
            'url' => $this->url,
        );
        if ($this->image) {
            $data['image'] = $this->image;
        }
        $where = array('username' => $this->username,);
        $result = $this->SelectByCond($where)->toArray();
        if ($result) {
            $imgOr = $_SERVER['DOCUMENT_ROOT'] . "/upload/image/admin/original/" . $result['image'];
            $imgTh = $_SERVER['DOCUMENT_ROOT'] . "/upload/image/admin/thumbnail/" . $result['image'];
            @unlink($imgOr);
            @unlink($imgTh);
            return $tb->editByCond($data, array('id' => $this->id));
        } else {
            return $tb->insertInto($data);
        }
    }

    /**
     * 获取所有数据
     * @param select [admin]
     * @return MemberTable
     */
    public function SelectAll() {
        $tb = self::getTableGateway();
        return $tb->selectAll();
    }

    /**
     * 获取 where 条件下的数据
     * @param select-where [admin]
     * @return MemberTable
     */
    public function SelectByCond($where) {
        $tb = self::getTableGateway();
        return $tb->selectByCond($where);
    }

    /**
     * 修改数据
     * @param edit-where [admin]
     * @return MemberTable
     */
    public function EditByCond($data, $where) {
        $tb = self::getTableGateway();
        return $tb->editByCond($data, $where);
    }

    /**
     * 交互阵列
     */
    public function exchangeArray($data) {
        $this->id = (isset($data['id'])

                ) ? $data['id'] : null;
        $this->username = (isset($data['username'])) ? $data['username'] : null;
        $this->password = (isset($data['password'])) ? $data['password'] : null;
        $this->email = (isset($data['email'])) ? $data['email'] : null;
        $this->image = (isset($data['image'])) ? $data['image'] : null;
        $this->nickname = (isset($data['nickname'])) ? $data['nickname'] : null;
        $this->last_name = (isset($data['last_name'])) ? $data['last_name'] : null;
        $this->first_name = (isset($data['first_name'])) ? $data['first_name'] : null;
        $this->url = (isset($data['url'])) ? $data['url'] : null;
        $this->address = (isset($data['address'])) ? $data['address'] : null;
    }

    /**
     * 获取所有参数
     */
    public function toArray() {
        $array['id'] = $this->id;
        $array['username'] = $this->username;
        $array['password'] = $this->password;
        $array['email'] = $this->email;
        $array['image'] = $this->image;
        $array['nickname'] = $this->nickname;
        $array['last_name'] = $this->last_name;
        $array['first_name'] = $this->first_name;
        $array['url'] = $this->url;
        $array['address'] = $this->address;
        return $array;
    }

    /**
     * 析构函数 , 释放内存
     */
    public function __destruct() {
        $this->id = null;
        $this->username = null;
        $this->password = null;
        $this->email = null;
        $this->image = null;
        $this->nickname = null;
        $this->last_name = null;
        $this->first_name = null;
        $this->url = null;
        $this->address = null;
    }

}
